package com.atguigu.list;

public class LinkedList<E> {

    private class Node {
        public E e;
        public Node next;

        public Node(E e, Node next) {
            this.e = e;
            this.next = next;
        }

        public Node(E e) {
            this(e, null);
        }

        public Node() {
            this(null, null);
        }

        @Override
        public String toString() {
            return e.toString();
        }
    }

    private Node head;
    private int size;

    public LinkedList() {
        head = new Node(null, null);//虚拟头结点
        size = 0;//节点数量
    }

    public int getSize() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    //在链表的index位置添加元素e
    public void add(int index, E e) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("Add Faild , Illgeal Index");
        }
        Node prev = head;
        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }
        prev.next = new Node(e, prev.next);
        size++;
    }

    //在链表头部添加元素e
    public void addFirst(E e) {
        add(0, e);
    }

    //向链表尾部添加元素
    public void addLast(E e) {
        add(size, e);
    }

    //获得链表index位置元素
    public E get(int index) {
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("Get faild , Illegal index");
        }
        Node curNode = head.next;
        for (int i = 0; i < index; i++) {
            curNode = curNode.next;
        }
        return curNode.e;
    }

    //获得链表第一个元素
    public E getFirst() {
        return get(0);
    }

    //获得链表最后一个元素
    public E getLast() {
        return get(size - 1);
    }

    //修改链表第index位置元素
    public void set(int index, E e) {
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("Set faild , Illegal index");
        }
        Node curNode = head.next;
        for (int i = 0; i < index; i++) {
            curNode = curNode.next;
        }
        curNode.e = e;
    }

    //查找链表中是否有元素e
    public boolean contains(E e) {
        for (Node cur = head.next; cur != null; cur = cur.next) {
            if (cur.e.equals(e)) {
                return true;
            }
        }
        return false;
    }

    public void removeElement(E e) {
        removeElement(head, e);
    }

    private Node removeElement(Node head, E e) {
        if (head == null) {
            return null;
        }
        if (head.e.equals(e)) {
            size--;
            return head.next;
        }
        head.next = removeElement(head.next, e);
        return head;
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        for (Node cur = head.next; cur != null; cur = cur.next) {
            res.append(cur).append(" -> ");
        }
        res.append("NULL");
        return res.toString();
    }

    //从链表中删除index位置元素
    public E remove(int index) {
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("remove failed , index illegal");
        }
        Node prev = head;
        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }
        Node delNode = prev.next;
        prev.next = delNode.next;
        delNode.next = null;// help GC
        size--;
        return delNode.e;
    }

    //删除链表头元素
    public E removeLast() {
        return remove(size - 1);
    }

    //删除链表尾元素
    public E removeFirst() {
        return remove(0);
    }
}
